<!--
 * @Descripttion: 
 * @version: 
 * @Author: mmzhang
 * @Date: 2021-07-21 10:45:02
 * @LastEditTime: 2023-02-03 13:55:59
-->
<template>
  <div class="enclosure-list">
    <dt-search ref="search" :labelWidth="120" @reset="resetFun" @change="onValueChange">
      <Button type="primary" @click="exportAttachFun()"
        v-if="unlocking('0104010202')">下载附件</Button>
      <Button type="primary" @click="operationClick"
        v-if="unlocking('0104010204')">附件下载记录</Button>
    </dt-search>

    <dt-grid ref="table" :page="page" :onFetch='onFetch' :select="{change:selectionChange}" />
    <!--图片预览-->
    <preview-image :images="preImgs" :showDownLoad="true" ref="previewImage"></preview-image>
    <!--客户详情-->
    <Modal v-model="detailsFlag" title="客户详情" width="80%" :styles="{top:'5%'}"
      :footer-hide="true">
      <detail-page v-if="detailsFlag" :detailsParams="detailsParams"></detail-page>
    </Modal>
    <!--附件下载记录-->
    <operation-record ref="operationRecord"></operation-record>
  </div>
</template>
<script>
  import encloseColumn from './enclosureColumn'
  import subMgr from '@/biz/sales/subOrder'
  import searchOpt from './search'
  import detailPage from '../../../allCustomersManagement/details.vue'
  import dt from '@dt/dt'
  import comMgr from '@/biz/sales/salesCommon'
  export default {
    props: {
      value: Object
    },
    data() {
      return {
        detailsFlag: false,
        detailsParams: {},
        isFileList: true,
        searchInfo: {
          statusList: [], //状态
          productIds: [], //产品
          buildingIds: [],
          unitIds: [],
          attachmentUpload: '0'
        },
        page: {
          rows: 50,
          current: 1
        },
        selectionList: [],
        tableData: [],
        preImgs: [],
        countType: 0
      }
    },
    created() {},
    components: {
      detailPage,
    },
    mounted() {
      let _this = this
      let retainParams = dt.memory.get('subOrder_params')

      this.$refs.table.init(this, new encloseColumn(this))
      this.$refs.search.init(this.$refs.table, new searchOpt(this), this
        .searchInfo)

      if (this.value.search) {
        Object.keys(this.searchInfo).map(function(key) {
          _this.value.search[key] && (_this.searchInfo[key] = _this.value.search[key]) //比较key
        })
      }

      // if (this.value.search) {
      //   this.$refs.search.update(new searchOpt(this), this.value.search)
      // }

      this.$nextTick(function() {
        this.$refs.table.throwFilter()?.import(this.value.filter)
        if (retainParams) {
          Object.keys(this.searchInfo).map(function(key) {
            retainParams[key] && (_this.searchInfo[key] = retainParams[key]) //比较key
          })
          retainParams.rows && (this.page.rows = retainParams.rows)
          retainParams.page && (this.page.current = retainParams.page)
          retainParams.filter && _this.$refs.table.throwFilter().import(retainParams
            .filter)
        }
      })
      this.$refs.table.fetch()
    },
    methods: {
      // 导出记录
      operationClick() {
        this.$refs.operationRecord.show('fileDownload_subEnclosureList', 'M_18', this.$dt
          .buildInfo().housesId)
      },
      resetFun() {
        this.$refs.search.init(this.$refs.table, new searchOpt(this).options(), this
          .searchInfo)
      },
      onValueChange() {
        this.$emit('input', { search: this.$refs.table.searchInfo, filter: this.$refs.table.throwFilter().export()})
      },
      onFetch(data) {
        data.housesId = dt.buildInfo().housesId
        data.isWeb = true

        this.onValueChange()
        this.filterParams = {
          ...data
        }
        return subMgr.getSubAttachList(data).then(res => {
          this.tableData = res.list
          return res
        }).catch(err => {
          this.tableData = []
        })
      },
      //列表选中
      selectionChange(selection) {
        this.selectionList = selection
      },
      // 保留列表查询参数
      retainParamsFun() {
        let params = Object.assign({
            showTab: 'enclosureList',
            filter: this.$refs.table.throwFilter().export()
          },
          this.filterParams
        )
        dt.memory.set('subOrder_params', params)
      },
      // 客户姓名链接到客户资料
      onLinkUser(row) {
        if (row.customerName == '-') {
          return false
        }
        this.detailsFlag = true
        this.detailsParams = {
          customerId: row.customerId,
          userCustomerId: row.userCustomerId,
          buildingId: dt.buildInfo().housesId,
          userId: dt.userInfo().userId,
          cityCode: dt.buildInfo().cityId,
        }
      },

      // 列表图片预览
      preViewImg(check) {
        let row = check[0]
        let numType = check[1]
        let attachType = check[2]
        if (
          row[numType] == '0' ||
          row[numType] == '-' ||
          (isNaN(row[numType]) && !isNaN(Date.parse(row[numType])))
        ) {
          return false
        }
        let params = {
          attachType: attachType,
          id: row.orderId,
          type: 1,
        }
        this.preImgs = []
        comMgr
          .preViewImg(params)
          .then((res) => {
            if (res) {
              this.preImgs = res
              this.$refs.previewImage.show()
            }
          })
          .catch((res) => {})
      },

      resolveImgShow(imgs) {
        return imgs == '-' ?
          0 :
          isNaN(imgs) && !isNaN(Date.parse(imgs)) ?
          imgs + '补' :
          imgs
      },

      resolveDateShow(imgs) {
        return isNaN(imgs) && !isNaN(Date.parse(imgs))
      },

      // 查看
      detailFun(row) {
        let orderParams = {
          orderNum: row.orderCode, // 单号
          businessId: row.orderId, // 业务id
          buildingId: this.$dt.buildInfo().housesId,
          subscriptionId: ''
        }

        this.linkOrderFun(orderParams)
      },
      // 导出附件
      exportAttachFun(row) {
        let params = {}
        if (row) {
          params = {
            orderCodeList: [row.orderCode],
            housesId: dt.buildInfo().housesId,
          }
        } else if (this.selectionList.length) {
          params = {
            orderCodeList: this.selectionList.map((item) => item.orderCode),
            housesId: dt.buildInfo().housesId,
          }
        } else {
          params = {
            orderCodeList: this.tableData.map((item) => item.orderCode),
            housesId: dt.buildInfo().housesId
          }
        }

        subMgr
          .exportAttach(params)
          .then((res) => {})
          .catch((err) => {})
      }
    },
  }
</script>
<style lang="scss" scoped>
  .enclosure-list {
    .sub-total {
      line-height: 40px;
    }
  }
</style>